<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\demo\controller;

use cmf\controller\RestBaseController;
use EasyWeChat\Factory;

class DemoController extends RestBaseController
{
  

    public function index()
    {
        $config = [
            'app_id' => 'wxeeba4cf409977357',
            'secret' => 'fce11b117dd886149c011b2eb3dc5e20',
            'response_type' => 'array',
        //...
        ];

        $app = Factory::miniProgram($config);
        dump($app);die;
        return json(['code'=>1,'msg'=>'dfdsfdsf']);
    }
    

    public function get_oauth()
    {
        $config = [
            'app_id' => 'wxeeba4cf409977357',
            'secret' => 'fce11b117dd886149c011b2eb3dc5e20',
            'response_type' => 'array',
        //...
        ];

        $app = Factory::miniProgram($config);
        $post = input('post.');
        $code = $post['code'];
        $post['openId']='';
        $iv=$post['iv'];
        $encryptedData=$post['encryptedData'];
        $session = $app->auth->session($code);//正确返回 session_key，openid，unionid，否则 返回：errcode
        $res=$app->encryptor->decryptData($session['session_key'], $iv, $encryptedData);

        if($res['openId'])
        {
            $this->save_user_info('');
        }
    }

    public function save_user_info($data)
    {
        //存数据
    }

}